package org.xiaov.core.tree;

import org.springframework.util.ReflectionUtils;
import org.xiaov.core.bean.JSONHelper;
import org.xiaov.core.string.StringHelper;
import org.xiaov.execption.XiaovTreeException;

import java.lang.reflect.Field;
import java.util.*;

/**
 * <p>
 * 树形帮助类
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/4/4 21:51
 */
public class TreeHelper {

    /**
     * 默认的root节点的id
     */
    private static final String DEFAULT_PARENT_ID = "0";
    /**
     * 默认实体的默认字段
     */
    private static final String DEFAULT_ID_FIELD_NAME = "id";
    /**
     * 默认实体的父节点
     */
    private static final String DEFAULT_PARENT_ID_FIELD_NAME = "pid";
    /**
     * 默认实体的子节点集合
     */
    private static final String DEFAULT_CHILDREN_FIELD_NAME = "children";

    /**
     * @param origList 要转换的 List
     * @param <T>      拥有父子结构的 Entity
     * @return 树形结果
     * @throws XiaovTreeException .
     */
    public static <T> List<T> convert(List<T> origList) {
        return convert(origList, DEFAULT_ID_FIELD_NAME, DEFAULT_PARENT_ID_FIELD_NAME, DEFAULT_CHILDREN_FIELD_NAME);
    }

    /**
     * 将 List 转为树形结构
     *
     * @param origList          : 要转换的 List
     * @param idFieldName       : id字段名
     * @param parentIdFieldName : parentId 字段名
     * @param childrenFieldName : children 字段名
     * @param <T>               : 拥有父子结构的 Entity
     * @return : 树形结果
     * @throws XiaovTreeException .
     */
    public static <T> List<T> convert(List<T> origList, String idFieldName,
                                      String parentIdFieldName, String childrenFieldName) {
        // 用于保存当前 id 索引的实体类
        Map<String, T> idMaps = new HashMap<>(16);
        // 暂存区, 用于保存没有找到父 id 的控件
        List<T> tempList = new ArrayList<>();
        List<T> result = new ArrayList<>();
        for (T entity : origList) {
            // 获取 id, parentId, children
            String id = Objects.toString(getFieldValue(entity, idFieldName), "");
            String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
            if (StringHelper.isEmpty(id)) {
                throw new XiaovTreeException("存在id为空的资料");
            }
            idMaps.put(id, entity);
            if (DEFAULT_PARENT_ID.equals(parentId) || StringHelper.isEmpty(parentId)) {
                // 如果父 id 为0或为空, 则实体类为第一层
                result.add(entity);
            } else {
                // 根据父 id 获取实体类
                T parentEntity = idMaps.get(parentId);
                if (parentEntity == null) {
                    // 没找到先放入暂存区
                    tempList.add(entity);
                } else {
                    // 父组件判断是否存在 children, 不存在新增, 存在则直接加入
                    setChildrenValue(childrenFieldName, entity, parentEntity);
                }
            }
        }
        // 处理暂存区, 暂存区的一定不为根节点, 所以它只要父节点存在, 那么此轮查询一定能找到父节点(上一轮已经将全部节点放入 idMaps)
        for (T entity : tempList) {
            // 获取 parentId
            String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
            // 根据父id获取实体类
            T parentEntity = idMaps.get(parentId);
            if (parentEntity == null) {
                throw new XiaovTreeException("存在孤立的子节点");
            } else {
                // 父组件判断是否存在children, 不存在新增, 存在则直接假如
                setChildrenValue(childrenFieldName, entity, parentEntity);
            }
        }
        return result;
    }

    /**
     * 父组件判断是否存在children, 不存在新增, 存在则直接假如
     *
     * @param childrenFieldName children 字段名
     * @param entity            实体
     * @param parentEntity      父实体
     * @param <T>               拥有父子结构的 Entity
     * @throws XiaovTreeException 树形异常
     */
    @SuppressWarnings("unchecked")
    private static <T> void setChildrenValue(String childrenFieldName, T entity, T parentEntity) {
        Object children = getFieldValue(parentEntity, childrenFieldName);
        List<T> childrenList;
        if (children == null) {
            childrenList = new ArrayList<>();
            childrenList.add(entity);
            setFieldValue(parentEntity, childrenFieldName, childrenList);
        } else {
            List<T> childrenReal = (List<T>) children;
            childrenReal.add(entity);
        }
    }

    /**
     * 获取字段的值
     *
     * @param entity
     * @param fieldName
     * @param <T>
     * @return
     * @throws XiaovTreeException .
     */
    private static <T> Object getFieldValue(T entity, String fieldName) {
        Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
        if (field == null) {
            throw new XiaovTreeException(String.format("字段名称[%s]不存在", fieldName));
        }
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        Object result = ReflectionUtils.getField(field, entity);
        field.setAccessible(accessible);
        return result;
    }

    /**
     * 给字段写入值
     *
     * @param entity
     * @param fieldName
     * @param value
     * @param <T>
     * @throws XiaovTreeException
     */
    private static <T> void setFieldValue(T entity, String fieldName, Object value) {
        Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
        if (field == null) {
            throw new XiaovTreeException(String.format("字段名称[%s]不存在", fieldName));
        }
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        ReflectionUtils.setField(field, entity, value);
        field.setAccessible(accessible);
    }

    /**
     * 将树形结构转化成json
     *
     * @param origList
     * @param <T>
     * @return
     */
    public static <T> String convert2JSON(List<T> origList) {
        List<T> list = convert(origList, DEFAULT_ID_FIELD_NAME, DEFAULT_PARENT_ID_FIELD_NAME, DEFAULT_CHILDREN_FIELD_NAME);
        return JSONHelper.toJSONString(list);
    }

    /**
     * 将树形结构转化成json
     *
     * @param origList
     * @param idFieldName
     * @param parentIdFieldName
     * @param childrenFieldName
     * @param <T>
     * @return
     */
    public static <T> String convert2JSON(List<T> origList, String idFieldName,
                                          String parentIdFieldName, String childrenFieldName) {
        List<T> list = convert(origList, idFieldName, parentIdFieldName, childrenFieldName);
        return JSONHelper.toJSONString(list);
    }
}